import wx
from Constant import *
'''
GameObj_StoneWall_Stretch

    The game object of stretchable stone wall. According to stretchType, decide it's stretchable aspects.
    
    Once the object is created, it will start to check whether there is some space to stretch to in stretchable
    
    aspect. If so, it will start to stretch until encounter the obstacle, and the area it stretches to 
    
    will be turn to object - stone wall. Although the stretched areas burst, it still can be filled in 
    
    except this object - original stretching area bursts.
'''
class GameObj_StoneWall_Stretch():
    '''
    Constructor : GameObj_StoneWall_Stretch
        mainMap - object pointer MainMap
        stretchType - type of stretching
        posInMap - the position in map (row, column)
    '''
    def __init__(self, mainMap, stretchType, posInMap):
        self._001_initialInstanceVariable(mainMap, stretchType, posInMap)
        self._002_bindAction()
        self.ACT001_startTimer()
    '''
    function _001 : initialize the variables used for this object
        mainMap - object pointer MainMap
        stretchType - type of stretching
        posInMap - the position in map (row, column)
    '''
    def _001_initialInstanceVariable(self, mainMap, stretchType, posInMap):
        #object pointer - MainMap
        self.mainMap = mainMap
        #record the stretching type
        self.stretchType = stretchType
        #record current position in map (row, column)
        self.posInMap = posInMap
        #record all location of stone wall that stretched from the center point to left
        self.listPos_StretchLeft = []
        #record all location of stone wall that stretched from the center point to right
        self.listPos_StretchRight = []
        #timer of stretching to left
        self.timer_stretchLeft = wx.Timer()
        #timer of stretching to right
        self.timer_stretchRight = wx.Timer()
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        if self.stretchType == STONE_WALL_STRETCH_LEFT_RIGHT :
            self.timer_stretchLeft.Bind(wx.EVT_TIMER, self.SLOT002_stretchLeft)
            self.timer_stretchRight.Bind(wx.EVT_TIMER, self.SLOT001_stretchRight)
        self.mainMap.evtCollector.Bind(EVT_DESTROY_GAME_OBJ, self.SLOT003_destroy)
    '''
    function ACT001 : start timer of this class
    '''
    def ACT001_startTimer(self):
        if self.stretchType == STONE_WALL_STRETCH_LEFT_RIGHT :
            self.timer_stretchLeft.Start(1000)
            self.timer_stretchRight.Start(1000)
    '''
    function ACT002 : destroy all object of this class
    '''  
    def ACT002_destroyAll(self):
        self.timer_stretchLeft.Stop()
        self.timer_stretchLeft.Destroy()
        self.timer_stretchRight.Stop()
        self.timer_stretchRight.Destroy()
        del self
    '''
    function ACT003 : judge and stretch from position of this object
        dir : direction of stretching
        listEverStretch : the list of recording the position that have ever stretched to in direction - dir
    ''' 
    def ACT003_stretch(self, dir, listEverStretch):
        curPos = [self.posInMap[0] + dir[0], self.posInMap[1] + dir[1]]
        isStretch = False
        while not isStretch :
            objType = self.mainMap.GET001_objTypeIn_listCube(curPos)
            if self.mainMap.ACT004_judgePosIsOutOfRange(curPos) :  #the position is illegal
                isStretch = True
            elif (not LIST_IS_OBSTACLE[str(GAME_OBJ_STONE_WALL_STRETCH)][objType]) : #encounter the none-obstacle
                listEverStretch.append(curPos)
                self.mainMap.ACT003_changeObj(curPos, GAME_OBJ_STONE_WALL, 0)
                isStretch = True
            elif objType == GAME_OBJ_STONE_WALL :  #if the current object is stone wall that is stretched,start next
                if (not listEverStretch.__contains__(curPos)) :
                    isStretch = True
            elif LIST_IS_OBSTACLE[str(GAME_OBJ_STONE_WALL_STRETCH)][objType]:  #encounter the obstacle
                isStretch = True
            curPos = (curPos[0] + dir[0], curPos[1] + dir[1])
    '''
    function SLOT001 : slot of stretching in right direction
    ''' 
    def SLOT001_stretchRight(self, event):
        self.ACT003_stretch((0,1), self.listPos_StretchRight)
    '''
    function SLOT002 : slot of stretching in left direction
    ''' 
    def SLOT002_stretchLeft(self, event):
        self.ACT003_stretch((0,-1), self.listPos_StretchLeft)
    '''
    function SLOT003 : slot of destroying
    ''' 
    def SLOT003_destroy(self, event):
        if not event.isClearAll :
            if event.delObjType == GAME_OBJ_STONE_WALL_STRETCH and self.posInMap[0] == event.delPos[0] and \
            self.posInMap[1] == event.delPos[1] :
                self.ACT002_destroyAll()
        else :
            self.ACT002_destroyAll()
        event.Skip()